<?php

class Ext_Ucenter
{

    public function __construct()
    {
        if(defined("UC_API"))
        {
            return "";
        }
        
        $config = Ext_Config::getInstance()->getConfig('ucenter');
        
        define('UC_CONNECT', $config->ucenter->UC_DBCONNECT);         // mysql 是直接连接的数据库, 为了效率, 建议采用 mysql
        define('UC_DBCONNECT', $config->ucenter->UC_DBCONNECT);         // mysql 是直接连接的数据库, 为了效率, 建议采用 mysql
        define('UC_DBHOST', $config->ucenter->UC_DBHOST);           // UCenter 数据库主机
        define('UC_DBUSER', $config->ucenter->UC_DBUSER);           // UCenter 数据库用户名
        define('UC_DBPW', $config->ucenter->UC_DBPW);               // UCenter 数据库密码
        define('UC_DBNAME', $config->ucenter->UC_DBNAME);           // UCenter 数据库名称
        define('UC_DBCHARSET', $config->ucenter->UC_DBCHARSET);     // UCenter 数据库字符集
        define('UC_DBTABLEPRE', $config->ucenter->UC_DBTABLEPRE);   // UCenter 数据库表前缀
        define('UC_KEY', $config->ucenter->UC_KEY);                 // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
        define('UC_API', $config->ucenter->UC_API);                 // UCenter 的 URL 地址, 在调用头像时依赖此常量
        define('UC_CHARSET', $config->ucenter->UC_CHARSET);         // UCenter 的字符集
        define('UC_IP', $config->ucenter->UC_IP);                   // UCenter 的 IP
        define('UC_APPID', $config->ucenter->UC_APPID);             // 当前应用的 ID
        define('UC_PPP', $config->ucenter->UC_PPP);             // 当前应用的 ID
        
        require_once 'uc_client/client.php';
        
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        $options = $bootstrap->getApplication()->getOptions();
        if(isset($options['phpSettings']['error_reporting']))
        {
            error_reporting($options['phpSettings']['error_reporting']);
        }
    }
    
    public function loginByEmail($email, $password)
    {
        require_once 'Ext/Account/Exception.php';
        
        $optionsRequired = array('email', 'password');
        foreach ($optionsRequired as $optionRequired) {
            if (null === ${$optionRequired}) {
                throw new Ext_Account_Exception("Option '$optionRequired' must be set before login");
            }
        }

        $result = uc_user_login($email, $password, 2, 0);

        return $result;
    }
    
    public function loginByUsername($username, $password)
    {
        require_once 'Ext/Account/Exception.php';
        
        $optionsRequired = array('username', 'password');
        foreach ($optionsRequired as $optionRequired) {
            if (null === ${$optionRequired}) {
                throw new Ext_Account_Exception("Option '$optionRequired' must be set before login");
            }
        }

        $result = uc_user_login($username, $password, 0, 0);

        return $result;
    }
    
    public function register($username, $password, $email)
    {
        require_once 'Ext/Account/Exception.php';
        
        $optionsRequired = array('username', 'password', 'email');
        foreach ($optionsRequired as $optionRequired) {
            if (null === ${$optionRequired}) {
                throw new Ext_Account_Exception("Option '$optionRequired' must be set before register");
            }
        }
    
        $uid = uc_user_register($username, $password, $email);
        
        return $uid;
    }
    
    public function changeEmail($username, $email)
    {
        require_once 'Ext/Account/Exception.php';
        
        $optionsRequired = array('username', 'email');
        foreach ($optionsRequired as $optionRequired) {
            if (null === ${$optionRequired}) {
                throw new Ext_Account_Exception("Option '$optionRequired' must be set before edit email.");
            }
        }
    
        $result = uc_user_edit($username, null, null, $email, 1);
        
        return $result;
    }
    
    public function changePassword($username, $password)
    {
        require_once 'Ext/Account/Exception.php';
        
        $optionsRequired = array('username', 'password');
        foreach ($optionsRequired as $optionRequired) {
            if (null === ${$optionRequired}) {
                throw new Ext_Account_Exception("Option '$optionRequired' must be set before register");
            }
        }
    
        $result = uc_user_edit($username, null, $password, null, 1);
        
        return $result;
    }
    
    public function show($username)
    {
        require_once 'Ext/Account/Exception.php';
        
        $optionsRequired = array('username');
        foreach ($optionsRequired as $optionRequired) {
            if (null === ${$optionRequired}) {
                throw new Ext_Account_Exception("Option '$optionRequired' must be set before gain");
            }
        }
    
        $result = uc_get_user($username);
        
        return $result;
    }
}
